//
// Created by 吴喆 on 2020/3/5.
//

#include "FileUtil.h"


IMG_TYPE FileUtil::isImage(std::ifstream &fStream) {

    char head[4];

    fStream.get(head, 4);
    fStream.seekg(0, std::ios::beg);
    if (fStream.get() == 0xFF && fStream.get() == 0xD8 && fStream.get() == 0xFF) {
//        std::cout << filePath << " IT IS A JPEG" << std::endl;
        return IMG_TYPE::JPEG;
    }
    fStream.seekg(0, std::ios::beg);
    if (fStream.get() == 0x89 && fStream.get() == 0x50 && fStream.get() == 0x4E && fStream.get() == 0x47) {
//        std::cout << filePath << " IT IS A PNG" << std::endl;
        return IMG_TYPE::PNG;
    }
    fStream.seekg(0, std::ios::beg);
    if (fStream.get() == 0x47 && fStream.get() == 0x49 && fStream.get() == 0x46 && fStream.get() == 0x38) {
//        std::cout << filePath << " IT IS A GIF" << std::endl;
        return  IMG_TYPE::GIF;
    }
    fStream.seekg(0, std::ios::beg);
    if (fStream.get() == 0x4D && fStream.get() == 0x4D && fStream.get() == 0x00 && fStream.get() == 0x2A) {
//        std::cout << filePath << " IT IS A TIF" << std::endl;
        return IMG_TYPE::UNKNOWN;
    }

    fStream.seekg(0, std::ios::beg);
    return IMG_TYPE::UNKNOWN;
}

